﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CMS_Admin_block_VoteSet : System.Web.UI.Page
{
    Usas.BLL.Cms.cms_block cb = new Usas.BLL.Cms.cms_block();
    Usas.BLL.Cms.cms_block_common_detail cbcd = new Usas.BLL.Cms.cms_block_common_detail();
    Usas.BLL.Cms.cms_block_common_detail_option cop = new Usas.BLL.Cms.cms_block_common_detail_option();

    Usas.Model.Cms.cms_block_common_detail cbcdm = new Usas.Model.Cms.cms_block_common_detail();
    Usas.Model.Cms.cms_block_common_detail_option copm = new Usas.Model.Cms.cms_block_common_detail_option();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "add":
                    save.Visible = false;
                    rbStatus.Enabled = false;
                    rbStatus.SelectedValue = "2";
                    DivContent.Visible = false;
                    break;
                case "edit":
                    add.Visible = false;
                    Bind();
                    break;
            }
        }
    }

    private void Bind()
    {
        cbcdm = cbcd.GetModel(Convert.ToInt32(Request["cid"]));
        txtName.Text = cbcdm.c_value;
        rbStatus.SelectedValue = cbcdm.c_view.ToString();
        addoption.Visible = false;

        OptionBind();
    }

    private void OptionBind()
    {
        DataSet ds = cop.GetList("o_c_id=" + Request["cid"].ToString());
        RptList.DataSource = ds;
        RptList.DataBind();
    }

    protected void add_Click(object sender, EventArgs e)
    {
        cbcdm.c_view = Convert.ToInt32(rbStatus.SelectedValue);
        cbcdm.c_value = txtName.Text.Trim();
        cbcdm.c_type = Request["page"].ToString();
        cbcdm.c_b_id = Convert.ToInt32(Request["control"]);
        cbcdm.c_des = "";
        int Nid = cbcd.Add(cbcdm);
        if (Nid > 1)
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('添加成功');location.href='" + "VoteSet.aspx?action=edit&page=" 
                + Request["page"].ToString() + "&control=" + Request["control"].ToString() + "&cid=" + Nid + "'", true);

        }
    }

    protected void save_Click(object sender, EventArgs e)
    {
        cbcdm.c_view = Convert.ToInt32(rbStatus.SelectedValue);
        cbcdm.c_value = txtName.Text.Trim();
        cbcdm.c_type = Request["page"].ToString();
        cbcdm.c_b_id = Convert.ToInt32(Request["control"]);
        cbcdm.c_id = Convert.ToInt32(Request["cid"]);
        cbcdm.c_des = "";
        cbcd.Update(cbcdm);

        ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('修改成功');location.href='" + "VoteSet.aspx?action=edit&page=" 
            + Request["page"].ToString() + "&control=" + Request["control"].ToString() + "&cid=" + Request["cid"].ToString() +"'", true);

    }

    private void EditOption(string id)
    {
        addoption.Visible = true;
        option_add.Visible = false;
        option_edit.Visible = true;

        option_edit.CommandArgument = id.Split('|')[0];
        option_name.Text = id.Split('|')[1];
    }

    protected void NewOption_Click(object sender, EventArgs e)
    {
        addoption.Visible = true;
        option_edit.Visible = false;
        option_add.Visible = true;
    }

    protected void RptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            LinkButton Del = (LinkButton)e.Item.FindControl("Del");
            Del.CommandName = "del";
            Del.CommandArgument = drv["o_id"].ToString();

            LinkButton Edit = (LinkButton)e.Item.FindControl("Edit");
            Edit.CommandName = "edit";
            Edit.CommandArgument = drv["o_id"].ToString() + "|" + drv["o_name"].ToString();
        }
    }

    protected void option_add_Click(object sender, EventArgs e)
    {
        copm.o_c_id = Convert.ToInt32(Request["cid"]);
        copm.o_name = option_name.Text.Trim();
        copm.o_value = "0";
        cop.Add(copm);
        Response.Redirect(Request.Url.ToString());

    }
    protected void RptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "del":
                cop.Delete(Convert.ToInt32(e.CommandArgument));
                Response.Redirect(Request.Url.ToString());

                break;
            case "edit":
                EditOption(e.CommandArgument.ToString());
                break;
        }
    }
    protected void option_edit_Click(object sender, EventArgs e)
    {
        copm.o_id = Convert.ToInt32(((Button)(sender)).CommandArgument);
        copm.o_name = option_name.Text.Trim();
        cop.Update(copm);
        Response.Redirect(Request.Url.ToString());
    }
    protected void option_cancel_Click(object sender, EventArgs e)
    {
        addoption.Visible = false;
    }
}
